package com.liu.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 我们后面需要自己写实现类注入进来
    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //传入UserDetailsService实现类和密码加密操作
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }

    @Bean
    public PasswordEncoder password(){
        //BCryptPasswordEncoder是PasswordEncoder接口的实现类，用于对密码进行加密操作
        return new BCryptPasswordEncoder();
    }

    // 设置要跳转自定义登录页面
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.formLogin()  //自定义自己编写的登录页面
//                .loginPage("/login.html") //登录页面设置
//                .loginProcessingUrl("/user/login") //登录访问路径
//                .defaultSuccessUrl("/test/index").permitAll() //登录成功之后
//                .and().authorizeRequests() //哪些可以访问，哪些不可以
//                .antMatchers("/","/test/hello","/user/login").permitAll() //设置哪些路径不需要验证
//                .anyRequest().authenticated() //所有请求都可以访问
//                .and().csrf().disable(); //关闭csrf防护
//    }


}
